//
//  HomeSectionThree.m
//  JiaoX
//
//  Created by yinqy on 15/1/29.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "HomeSectionThree.h"
#import "LeftLineView.h"
#import "HomeBanner.h"
#import "HomeFirstPageHeaderView.h"
#define TITLE_FONT_SIZE [DimensUtils getScaleX:14]

#define WIDTH_HEIGHT_RATIO 4.4444

@interface HomeSectionThree(){
    NSMutableArray *_btnArray;
    HomeBannerList *_bannerList;
}
@end
@implementation HomeSectionThree
- (instancetype)initWithHomeBannerList:(HomeBannerList *)bannerList{
    self = [super init];
    if (self) {
        _bannerList = bannerList;
        [self setBackgroundColor:[UIColor whiteColor]];
        _btnArray = [NSMutableArray array];
        LeftLineView *title = [[LeftLineView alloc]initWithTitleFontSize:TITLE_FONT_SIZE];
        [title setText:bannerList.title];
        title.label.textColor = JXColor(51, 51, 51);
        title.x = JXPublicInset;
        title.y = JXPublicInset;
        [self addSubview:title];
        
        CGFloat btnWidth = JXScreenW-JXPublicInset*2;
        CGFloat btnHeight = btnWidth/WIDTH_HEIGHT_RATIO;
        
        for (int i = 0; i<bannerList.list.count; i++) {
            CGFloat btnY = 0;
            if (_btnArray.count) {
                btnY = CGRectGetMaxY(((UIView *)[_btnArray lastObject]).frame)+JXPublicInset;
            }else{
                btnY = CGRectGetMaxY(title.frame)+JXPublicInset;
            }
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(JXPublicInset, btnY, btnWidth, btnHeight)];
            [btn setTag:i];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [btn sd_setBackgroundImageWithURL:[NSURL URLWithString:((HomeBanner *)bannerList.list[i]).bannerImg] forState:UIControlStateNormal];
            [self addSubview:btn];
            [_btnArray addObject:btn];
        }
        [self setSize:CGSizeMake(JXScreenW, CGRectGetMaxY(((UIView *)[_btnArray lastObject]).frame)+JXPublicInset)];
    }
    return self;
}

-(void)btnClick:(UIButton *)btn{
    HomeBanner *banner = _bannerList.list[btn.tag];
    if ([self.superview isKindOfClass:[HomeFirstPageHeaderView class]]) {
        HomeFirstPageHeaderView *view  =(HomeFirstPageHeaderView *) self.superview;
        if ([view.delegate respondsToSelector:@selector(sectionThreeClick:)]) {
            [view.delegate sectionThreeClick:banner.url];
        }
    }
}

@end
